<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="600"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      
      <a-form-item label="规则等级" field="rule_level">
        <sa-select v-model="formData.rule_level" dict="rulesLevel" placeholder="请选择规则等级" allow-clear  @change="handleRuleLevel" />
      </a-form-item>
      <a-form-item label="规则类型" v-if="formData.rule_level == 0" field="rule_type">
        <sa-select v-model="formData.rule_type" dict="rulesType" placeholder="请选择规则类型" allow-clear />
      </a-form-item>
      <a-form-item label="规则排序" field="rule_sort">
        <a-input v-model="formData.rule_sort" placeholder="请输入规则排序" />
      </a-form-item>
	  <a-form-item label="规则名称中文名称" field="rule_name_zh">
	    <a-input v-model="formData.rule_name_zh" placeholder="请输入规则名称中文名称" />
	  </a-form-item>
	  <a-form-item label="规则名称英文名称" field="rule_name_en">
	    <a-input v-model="formData.rule_name_en" placeholder="请输入规则名称英文名称" />
	  </a-form-item>
	  <a-form-item label="规则名称日语名称" field="rule_name_ja">
	    <a-input v-model="formData.rule_name_ja" placeholder="请输入规则名称日语名称" />
	  </a-form-item>
	  <a-form-item label="规则名称韩文名称" field="rule_name_ko">
	    <a-input v-model="formData.rule_name_ko" placeholder="请输入规则名称韩文名称" />
	  </a-form-item>
      <a-form-item label="所属规则名称" v-if="formData.rule_level == 1" field="parent_id">
		  <a-select
		    v-model="formData.parent_id"
		    :options="postData"        
		    allow-clear
		    placeholder="请选所属规则名称" />
      </a-form-item>
      <a-form-item label="是否必填" v-if="formData.rule_level == 0" field="is_required">
		  <sa-switch v-model="formData.is_required" checkedText="是"  uncheckedText="否"/>
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../api/rule'
import commonApi from '@/api/common'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')
const treeData = ref([])
const postData= ref([])


let title = computed(() => {
  return '匹配规则' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  rule_name_zh: '',
  rule_name_en: '',
  rule_name_ja: '',
  rule_id: null,
  rule_name_ko: '',
  rule_level: 0,
  rule_type: 0,
  rule_sort: 0,
  parent_id: 0,
  is_required: 0,
})

// 验证规则
const rules = {
  rule_name_zh: [{ required: true, message: '规则名称中文名称必需填写' }],
  rule_level: [{ required: true, message: '规则等级必需填写' }],
  rule_type: [{ required: true, message: '规则类型必需填写' }],
  is_required: [{ required: true, message: '是否必填必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {
  const resp = await api.getPageList()
  treeData.value = resp.data
  const postResp = await commonApi.commonGet('/matchingRule/MatchingRule/List')
  
  postData.value = postResp.data
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}
const handleRuleLevel=async(rule_level)=>{
	  
	if(rule_level==0){
		formData.parent_id=0
	}
}
// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.rule_id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.rule_id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
